package com.jdone.compus.repository;

import com.jdone.compus.model.ImeiRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface ImeiRecordRepository
        extends JpaRepository<ImeiRecord, Long>,
        JpaSpecificationExecutor<ImeiRecord> {
    List<ImeiRecord> findByNameContainingOrImeiContaining(String name, String imei);


    long countByCreatedAtBetween(LocalDateTime start, LocalDateTime end);

    long countByCreatedAtAfter(LocalDateTime start);

    long countByCreatedAtBefore(LocalDateTime end);

    @Query(value = "SELECT * FROM blow_imei_records WHERE id = :id AND is_deleted = :isDeleted", nativeQuery = true)
    Optional<ImeiRecord> findByIdAndIsDeleted(@Param("id") Long id, @Param("isDeleted") Integer isDeleted);

    Page<ImeiRecord> findByIsDeleted(Integer isDeleted, Pageable pageable);

    List<ImeiRecord> findByIsDeleted(Integer isDeleted);

    List<ImeiRecord> findByIsDeletedAndNameContainingOrImeiContaining(Integer isDeleted, String name, String imei);

}

